package com.lzyboyl.big.data.handle.task;

import com.lzyboyl.big.data.handle.util.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author linzf
 * @since 2021-08-02
 * 类描述： 基于redis的消费者的实现
 */
public class BigDataHandleTask implements Runnable {

    private Logger logger = LoggerFactory.getLogger(BigDataHandleTask.class);

    private RedisCache redisCache;

    public BigDataHandleTask(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    @Override
    public void run() {
        while (true) {
            String bigData = redisCache.rPop("BIG-DATA-READ", String.class);
            if (bigData == null || bigData.equals("")) {
                logger.info("当前队列暂无需要处理的数据");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                logger.info("需要处理的数据为：{}", bigData);
                // 此处模拟处理一个请求需要耗时10毫秒
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
